<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: samples/cpp/facedetect.cpp</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">samples/cpp/facedetect.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>This program demonstrates usage of the Cascade classifier class </p><div class="image">
<img src="../../Cascade_Classifier_Tutorial_Result_Haar.jpg" alt="Cascade_Classifier_Tutorial_Result_Haar.jpg" width="321" height="254"/>
<div class="caption">
Sample screenshot</div></div>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d8/da3/objdetect_8hpp.html">opencv2/objdetect.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d8/dcc/namespacestd.html">std</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> help(<span class="keyword">const</span> <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;\nThis program demonstrates the use of cv::CascadeClassifier class to detect objects (Face + eyes). You can use Haar or LBP features.\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;It&#39;s most known use is for faces.\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;Usage:\n&quot;</span></div><div class="line">        &lt;&lt;  argv[0]</div><div class="line">        &lt;&lt;  <span class="stringliteral">&quot;   [--cascade=&lt;cascade_path&gt; this is the primary trained classifier such as frontal face]\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;   [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;   [--scale=&lt;image scale greater or equal to 1, try 1.3 for example&gt;]\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;   [--try-flip]\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;   [filename|camera_index]\n\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;example:\n&quot;</span></div><div class="line">        &lt;&lt;  argv[0]</div><div class="line">        &lt;&lt;  <span class="stringliteral">&quot; --cascade=\&quot;data/haarcascades/haarcascade_frontalface_alt.xml\&quot; --nested-cascade=\&quot;data/haarcascades/haarcascade_eye_tree_eyeglasses.xml\&quot; --scale=1.3\n\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;During execution:\n\tHit any key to quit.\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tUsing OpenCV version &quot;</span> &lt;&lt; <a name="a0"></a><a class="code" href="../../da/dd7/core_2include_2opencv2_2core_2version_8hpp.html#a4f050efda432e041621b3af3df140adc">CV_VERSION</a> &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span> &lt;&lt; endl;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> detectAndDraw( <a name="_a1"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; img, <a name="_a2"></a><a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html">CascadeClassifier</a>&amp; cascade,</div><div class="line">                    <a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html">CascadeClassifier</a>&amp; nestedCascade,</div><div class="line">                    <span class="keywordtype">double</span> <a name="a3"></a><a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>, <span class="keywordtype">bool</span> tryflip );</div><div class="line"></div><div class="line"><span class="keywordtype">string</span> cascadeName;</div><div class="line"><span class="keywordtype">string</span> nestedCascadeName;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keyword">const</span> <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">    <a name="_a4"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> capture;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame, image;</div><div class="line">    <span class="keywordtype">string</span> inputName;</div><div class="line">    <span class="keywordtype">bool</span> tryflip;</div><div class="line">    <a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html">CascadeClassifier</a> cascade, nestedCascade;</div><div class="line">    <span class="keywordtype">double</span> <a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>;</div><div class="line"></div><div class="line">    <a name="_a5"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">cv::CommandLineParser</a> parser(argc, argv,</div><div class="line">        <span class="stringliteral">&quot;{help h||}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{cascade|data/haarcascades/haarcascade_frontalface_alt.xml|}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{nested-cascade|data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{scale|1|}{try-flip||}{@filename||}&quot;</span></div><div class="line">    );</div><div class="line">    <span class="keywordflow">if</span> (parser.<a name="a6"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a8c41cf242b2ad8c0472e3b57893ef200">has</a>(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        help(argv);</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    cascadeName = parser.<a name="a7"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a190215863579a6d244dccfd261ee0224">get</a>&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;cascade&quot;</span>);</div><div class="line">    nestedCascadeName = parser.<a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a190215863579a6d244dccfd261ee0224">get</a>&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;nested-cascade&quot;</span>);</div><div class="line">    scale = parser.<a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a190215863579a6d244dccfd261ee0224">get</a>&lt;<span class="keywordtype">double</span>&gt;(<span class="stringliteral">&quot;scale&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (scale &lt; 1)</div><div class="line">        scale = 1;</div><div class="line">    tryflip = parser.<a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a8c41cf242b2ad8c0472e3b57893ef200">has</a>(<span class="stringliteral">&quot;try-flip&quot;</span>);</div><div class="line">    inputName = parser.<a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a190215863579a6d244dccfd261ee0224">get</a>&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;@filename&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (!parser.<a name="a8"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#ad6b244a52f3b90da6fc463341c8a486f">check</a>())</div><div class="line">    {</div><div class="line">        parser.<a name="a9"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a70c31969ec5259b8c093d11a80c34f9f">printErrors</a>();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">if</span> (!nestedCascade.<a name="a10"></a><a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html#a1a5884c8cc749422f9eb77c2471958bc">load</a>(<a name="a11"></a><a class="code" href="../../d6/dba/group__core__utils__samples.html#ga4a37595bcd267558bd6c176c7307f050">samples::findFileOrKeep</a>(nestedCascadeName)))</div><div class="line">        cerr &lt;&lt; <span class="stringliteral">&quot;WARNING: Could not load classifier cascade for nested objects&quot;</span> &lt;&lt; endl;</div><div class="line">    <span class="keywordflow">if</span> (!cascade.<a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html#a1a5884c8cc749422f9eb77c2471958bc">load</a>(<a name="a12"></a><a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>(cascadeName)))</div><div class="line">    {</div><div class="line">        cerr &lt;&lt; <span class="stringliteral">&quot;ERROR: Could not load classifier cascade&quot;</span> &lt;&lt; endl;</div><div class="line">        help(argv);</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">if</span>( inputName.empty() || (isdigit(inputName[0]) &amp;&amp; inputName.size() == 1) )</div><div class="line">    {</div><div class="line">        <span class="keywordtype">int</span> camera = inputName.empty() ? 0 : inputName[0] - <span class="charliteral">&#39;0&#39;</span>;</div><div class="line">        <span class="keywordflow">if</span>(!capture.<a name="a13"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>(camera))</div><div class="line">        {</div><div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;Capture from camera #&quot;</span> &lt;&lt;  camera &lt;&lt; <span class="stringliteral">&quot; didn&#39;t work&quot;</span> &lt;&lt; endl;</div><div class="line">            <span class="keywordflow">return</span> 1;</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!inputName.empty())</div><div class="line">    {</div><div class="line">        image = <a name="a14"></a><a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga4a37595bcd267558bd6c176c7307f050">samples::findFileOrKeep</a>(inputName), <a name="a15"></a><a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a>);</div><div class="line">        <span class="keywordflow">if</span> (image.<a name="a16"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>())</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (!capture.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga4a37595bcd267558bd6c176c7307f050">samples::findFileOrKeep</a>(inputName)))</div><div class="line">            {</div><div class="line">                cout &lt;&lt; <span class="stringliteral">&quot;Could not read &quot;</span> &lt;&lt; inputName &lt;&lt; endl;</div><div class="line">                <span class="keywordflow">return</span> 1;</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        image = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>(<span class="stringliteral">&quot;lena.jpg&quot;</span>), <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a>);</div><div class="line">        <span class="keywordflow">if</span> (image.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>())</div><div class="line">        {</div><div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;Couldn&#39;t read lena.jpg&quot;</span> &lt;&lt; endl;</div><div class="line">            <span class="keywordflow">return</span> 1;</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( capture.<a name="a17"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a9d2ca36789e7fcfe7a7be3b328038585">isOpened</a>() )</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Video capturing has been started ...&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span>(;;)</div><div class="line">        {</div><div class="line">            capture &gt;&gt; frame;</div><div class="line">            <span class="keywordflow">if</span>( frame.empty() )</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">            <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame1 = frame.<a name="a18"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660">clone</a>();</div><div class="line">            detectAndDraw( frame1, cascade, nestedCascade, scale, tryflip );</div><div class="line"></div><div class="line">            <span class="keywordtype">char</span> c = (char)<a name="a19"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(10);</div><div class="line">            <span class="keywordflow">if</span>( c == 27 || c == <span class="charliteral">&#39;q&#39;</span> || c == <span class="charliteral">&#39;Q&#39;</span> )</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Detecting face(s) in &quot;</span> &lt;&lt; inputName &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">if</span>( !image.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() )</div><div class="line">        {</div><div class="line">            detectAndDraw( image, cascade, nestedCascade, scale, tryflip );</div><div class="line">            <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0);</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( !inputName.empty() )</div><div class="line">        {</div><div class="line">            <span class="comment">/* assume it is a text file containing the</span></div><div class="line"><span class="comment">            list of the image filenames to be processed - one per line */</span></div><div class="line">            FILE* f = fopen( inputName.c_str(), <span class="stringliteral">&quot;rt&quot;</span> );</div><div class="line">            <span class="keywordflow">if</span>( f )</div><div class="line">            {</div><div class="line">                <span class="keywordtype">char</span> buf[1000+1];</div><div class="line">                <span class="keywordflow">while</span>( fgets( buf, 1000, f ) )</div><div class="line">                {</div><div class="line">                    <span class="keywordtype">int</span> len = (int)strlen(buf);</div><div class="line">                    <span class="keywordflow">while</span>( len &gt; 0 &amp;&amp; isspace(buf[len-1]) )</div><div class="line">                        len--;</div><div class="line">                    buf[len] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">                    cout &lt;&lt; <span class="stringliteral">&quot;file &quot;</span> &lt;&lt; buf &lt;&lt; endl;</div><div class="line">                    image = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( buf, 1 );</div><div class="line">                    <span class="keywordflow">if</span>( !image.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() )</div><div class="line">                    {</div><div class="line">                        detectAndDraw( image, cascade, nestedCascade, scale, tryflip );</div><div class="line">                        <span class="keywordtype">char</span> c = (char)<a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0);</div><div class="line">                        <span class="keywordflow">if</span>( c == 27 || c == <span class="charliteral">&#39;q&#39;</span> || c == <span class="charliteral">&#39;Q&#39;</span> )</div><div class="line">                            <span class="keywordflow">break</span>;</div><div class="line">                    }</div><div class="line">                    <span class="keywordflow">else</span></div><div class="line">                    {</div><div class="line">                        cerr &lt;&lt; <span class="stringliteral">&quot;Aw snap, couldn&#39;t read image &quot;</span> &lt;&lt; buf &lt;&lt; endl;</div><div class="line">                    }</div><div class="line">                }</div><div class="line">                fclose(f);</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> detectAndDraw( <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; img, <a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html">CascadeClassifier</a>&amp; cascade,</div><div class="line">                    <a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html">CascadeClassifier</a>&amp; nestedCascade,</div><div class="line">                    <span class="keywordtype">double</span> <a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>, <span class="keywordtype">bool</span> tryflip )</div><div class="line">{</div><div class="line">    <span class="keywordtype">double</span> t = 0;</div><div class="line">    vector&lt;Rect&gt; faces, faces2;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">static</span> <a name="_a20"></a><a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> colors[] =</div><div class="line">    {</div><div class="line">        <a name="a21"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,0,0),</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,128,0),</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,255,0),</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0,255,0),</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0,128,255),</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0,255,255),</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0,0,255),</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,0,255)</div><div class="line">    };</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> gray, smallImg;</div><div class="line"></div><div class="line">    <a name="a22"></a><a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( img, gray, <a name="a23"></a><a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a> );</div><div class="line">    <span class="keywordtype">double</span> fx = 1 / <a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>;</div><div class="line">    <a name="a24"></a><a class="code" href="../../d5/df1/group__imgproc__hal__functions.html#ga2fe39d2201b12e1b961ca56b2aff9ff2">resize</a>( gray, smallImg, <a name="a25"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(), fx, fx, <a name="a26"></a><a class="code" href="../../da/d54/group__imgproc__transform.html#gga5bb5a1fea74ea38e1a5445ca803ff121ac00f4a8155563cdc23437fc0959da935">INTER_LINEAR_EXACT</a> );</div><div class="line">    <a name="a27"></a><a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga7e54091f0c937d49bf84152a16f76d6e">equalizeHist</a>( smallImg, smallImg );</div><div class="line"></div><div class="line">    t = (double)<a name="a28"></a><a class="code" href="../../db/de0/group__core__utils.html#gae73f58000611a1af25dd36d496bf4487">getTickCount</a>();</div><div class="line">    cascade.<a name="a29"></a><a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html#aaf8181cb63968136476ec4204ffca498">detectMultiScale</a>( smallImg, faces,</div><div class="line">        1.1, 2, 0</div><div class="line">        <span class="comment">//|CASCADE_FIND_BIGGEST_OBJECT</span></div><div class="line">        <span class="comment">//|CASCADE_DO_ROUGH_SEARCH</span></div><div class="line">        |<a name="a30"></a><a class="code" href="../../d5/d54/group__objdetect.html#gga3489c40a2fb178c477302403ec5cc1dba1d35c5234686c05c075d224efc91d673">CASCADE_SCALE_IMAGE</a>,</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(30, 30) );</div><div class="line">    <span class="keywordflow">if</span>( tryflip )</div><div class="line">    {</div><div class="line">        <a name="a31"></a><a class="code" href="../../d2/de8/group__core__array.html#gaca7be533e3dac7feb70fc60635adf441">flip</a>(smallImg, smallImg, 1);</div><div class="line">        cascade.<a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html#aaf8181cb63968136476ec4204ffca498">detectMultiScale</a>( smallImg, faces2,</div><div class="line">                                 1.1, 2, 0</div><div class="line">                                 <span class="comment">//|CASCADE_FIND_BIGGEST_OBJECT</span></div><div class="line">                                 <span class="comment">//|CASCADE_DO_ROUGH_SEARCH</span></div><div class="line">                                 |<a class="code" href="../../d5/d54/group__objdetect.html#gga3489c40a2fb178c477302403ec5cc1dba1d35c5234686c05c075d224efc91d673">CASCADE_SCALE_IMAGE</a>,</div><div class="line">                                 <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(30, 30) );</div><div class="line">        <span class="keywordflow">for</span>( vector&lt;Rect&gt;::const_iterator r = faces2.begin(); r != faces2.end(); ++r )</div><div class="line">        {</div><div class="line">            faces.push_back(<a name="a32"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(smallImg.<a name="a33"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> - r-&gt;x - r-&gt;width, r-&gt;y, r-&gt;width, r-&gt;height));</div><div class="line">        }</div><div class="line">    }</div><div class="line">    t = (double)<a class="code" href="../../db/de0/group__core__utils.html#gae73f58000611a1af25dd36d496bf4487">getTickCount</a>() - t;</div><div class="line">    printf( <span class="stringliteral">&quot;detection time = %g ms\n&quot;</span>, t*1000/<a name="a34"></a><a class="code" href="../../db/de0/group__core__utils.html#ga705441a9ef01f47acdc55d87fbe5090c">getTickFrequency</a>());</div><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> i = 0; i &lt; faces.size(); i++ )</div><div class="line">    {</div><div class="line">        <a name="_a35"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> r = faces[i];</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> smallImgROI;</div><div class="line">        vector&lt;Rect&gt; nestedObjects;</div><div class="line">        <a name="_a36"></a><a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a> center;</div><div class="line">        <a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> color = colors[i%8];</div><div class="line">        <span class="keywordtype">int</span> radius;</div><div class="line"></div><div class="line">        <span class="keywordtype">double</span> aspect_ratio = (double)r.<a name="a37"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>/r.<a name="a38"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>;</div><div class="line">        <span class="keywordflow">if</span>( 0.75 &lt; aspect_ratio &amp;&amp; aspect_ratio &lt; 1.3 )</div><div class="line">        {</div><div class="line">            center.<a name="a39"></a><a class="code" href="../../db/d4e/classcv_1_1Point__.html#a4c96fa7bdbfe390be5ed356edb274ff3">x</a> = <a name="a40"></a><a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>((r.<a name="a41"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a> + r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>*0.5)*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>);</div><div class="line">            center.<a name="a42"></a><a class="code" href="../../db/d4e/classcv_1_1Point__.html#a157337197338ff199e5df1a393022f15">y</a> = <a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>((r.<a name="a43"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a> + r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>*0.5)*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>);</div><div class="line">            radius = <a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>((r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a> + r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>)*0.25*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>);</div><div class="line">            <a name="a44"></a><a class="code" href="../../d9/db7/group__datasets__gr.html#gga610754124ced68d1f05760b5948fbb76a6f0d8b2d9e3e947b2a5c1eff9e81ee95">circle</a>( img, center, radius, color, 3, 8, 0 );</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">            <a name="a45"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( img, <a name="a46"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(<a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>(r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>), <a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>(r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>)),</div><div class="line">                       <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a>(<a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>((r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a> + r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>-1)*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>), <a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>((r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a> + r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>-1)*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>)),</div><div class="line">                       color, 3, 8, 0);</div><div class="line">        <span class="keywordflow">if</span>( nestedCascade.<a name="a47"></a><a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html#a1753ebe58554fe0673ce46cb4e83f08a">empty</a>() )</div><div class="line">            <span class="keywordflow">continue</span>;</div><div class="line">        smallImgROI = smallImg( r );</div><div class="line">        nestedCascade.<a class="code" href="../../d1/de5/classcv_1_1CascadeClassifier.html#aaf8181cb63968136476ec4204ffca498">detectMultiScale</a>( smallImgROI, nestedObjects,</div><div class="line">            1.1, 2, 0</div><div class="line">            <span class="comment">//|CASCADE_FIND_BIGGEST_OBJECT</span></div><div class="line">            <span class="comment">//|CASCADE_DO_ROUGH_SEARCH</span></div><div class="line">            <span class="comment">//|CASCADE_DO_CANNY_PRUNING</span></div><div class="line">            |<a class="code" href="../../d5/d54/group__objdetect.html#gga3489c40a2fb178c477302403ec5cc1dba1d35c5234686c05c075d224efc91d673">CASCADE_SCALE_IMAGE</a>,</div><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(30, 30) );</div><div class="line">        <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> j = 0; j &lt; nestedObjects.size(); j++ )</div><div class="line">        {</div><div class="line">            <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> nr = nestedObjects[j];</div><div class="line">            center.<a class="code" href="../../db/d4e/classcv_1_1Point__.html#a4c96fa7bdbfe390be5ed356edb274ff3">x</a> = <a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>((r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a> + nr.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a> + nr.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>*0.5)*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>);</div><div class="line">            center.<a class="code" href="../../db/d4e/classcv_1_1Point__.html#a157337197338ff199e5df1a393022f15">y</a> = <a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>((r.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a> + nr.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a> + nr.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>*0.5)*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>);</div><div class="line">            radius = <a class="code" href="../../db/de0/group__core__utils.html#ga085eca238176984a0b72df2818598d85">cvRound</a>((nr.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a> + nr.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>)*0.25*<a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>);</div><div class="line">            <a class="code" href="../../d9/db7/group__datasets__gr.html#gga610754124ced68d1f05760b5948fbb76a6f0d8b2d9e3e947b2a5c1eff9e81ee95">circle</a>( img, center, radius, color, 3, 8, 0 );</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <a name="a48"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( <span class="stringliteral">&quot;result&quot;</span>, img );</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:29 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
